package other.edan.newpaas.order;

import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEvent;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventRequest;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventResponse;
import com.rkhd.platform.sdk.data.model.Order;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import other.edan.newpaas.utils.HCXObjectService;
import other.edan.newpaas.utils.Result;
import other.edan.newpaas.utils.ResultVo;

/**
 * @Description: 订单提交审批前：校验测试卡金额
 * @Author: 黎炎松
 * @Date: 2025/3/4 17:53
 */
public class SubApproCheckCardAmount implements ApprovalEvent {

    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "订单提交审批前：校验测试卡金额 ==> ";


    @Override
    public ApprovalEventResponse execute(ApprovalEventRequest request) throws ScriptBusinessException {
        LOGGER.info(LOGGER_MSG + " 触发开始 ");
        LOGGER.info("ObjectApiKey:" + request.getEntityApiKey() + " , DataId:" + request.getDataId());
        LOGGER.info(request.toString());
        Result result = checkCardAmount(request.getDataId());

        ApprovalEventResponse response = new ApprovalEventResponse();
        response.setSuccess(result.getCode() == 200 ? true : false);
        response.setMsg(result.getMsg());
        return response;
    }

    public static void main(String[] args) {
        SubApproCheckCardAmount subApproCheckCardAmount = new SubApproCheckCardAmount();
        Result result = subApproCheckCardAmount.checkCardAmount(3703277346019940L);
        System.out.println("resultVo = " + result.getMsg());
    }

    /**
     * 订单提交审批前：校验测试卡金额
     * @param orderId
     * @return
     */
    public Result checkCardAmount(Long orderId) {
        LOGGER.info(LOGGER_MSG + "开始校验");
        Order order = new Order();
        order.setId(orderId);
        order = HCXObjectService.get(order);
        Integer chargesUndertake = order.getChargesUndertake__c();
        Double cardAmount = order.getCardAmount__c();

        LOGGER.info(LOGGER_MSG + "cardAmount = " + cardAmount);
        LOGGER.info(LOGGER_MSG + "chargesUndertake = " + chargesUndertake);

        if (cardAmount != null && cardAmount > 0 && cardAmount < 5000 && (chargesUndertake == null || chargesUndertake == 2)) {
            return Result.error("测试卡金额小于5000，请调整运费承担方");
        }

        return  Result.success();
    }
}
